﻿CREATE PROCEDURE [dbo].[AddAlert]
	@alert_rule_id int,
	@entity_id int,
	@entity_property_id int
AS
	if exists(select top 1 alert_id from dbo.Alert where alert_rule_id = @alert_rule_id and entity_id = @entity_id and entity_property_id = @entity_property_id and status != 255)
	begin
		-- Alert is already present and not closed. We only change the count
		update dbo.Alert set alert_count += 1 where alert_rule_id = @alert_rule_id and entity_id = @entity_id and entity_property_id = @entity_property_id and status != 255;
	end
	else
	begin
		-- This is a new alert
		insert into dbo.Alert (alert_rule_id, entity_id, entity_property_id) values
			(@alert_rule_id, @entity_id, @entity_property_id);
	end
